<?php
/**
 * Created by PhpStorm.
 * User: zq
 * Date: 2017/2/23
 * Time: 19:21
 */

namespace Common\Model;


use Org\Util\DataTree;

class GoodsModel extends BaseModel
{
    protected $pk = 'gid';//主键
    protected $tableName = 'goods';//指定操作数据表
    protected $_validate = [
//		array('gname','require','商品名称必须！',1,3),
//		array('gnumber','require','商品货号必须！'),
    ];

    //获取分类数据
    public function getCateData()
    {
        return (new DataTree())->tree((new CategoryModel())->select(), 'cname', 'cid', 'pid');
    }

    //获取品牌数据
    public function getBrandData()
    {
        return (new BrandModel())->select();
    }

    //添加
    public function store()
    {
//        echo '<pre>';
//        print_r($_POST);die;
        if (!$this->create()) {
            return ['valid' => false, 'message' => $this->getError()];
        }
        $detailModel = new DetailModel();
        if (!$detailModel->create($_POST)) {
            return ['valid' => false, 'message' => $detailModel->getError()];
        }
        //验证图册是否上传
        if(!isset($_POST['detailPic'])){
            return ['valid'=>false,'message'=>'请上传商品图册'];
        }
        //添加商品表
        $data['gname'] = I('post.gname');
        $data['gnumber'] = I('post.gnumber');
        $data['unit'] = I('post.unit');
        $data['marketprice'] = I('post.marketprice');
        $data['shopprice'] = I('post.shopprice');
        $data['total'] = I('post.total');
        $data['pic'] = I('post.pic');
        $data['click'] = I('post.click');
        $data['time'] = time();
        $data['bid'] = I('post.bid');
        $data['cid'] = I('post.cid');
        $data['tid'] = I('post.tid');
        $data['aid'] = 1;//管理员ID 取自session
        $gid = $this->add($data);
        //添加商品详情表
        $small ='';
        $medium ='';
        $big ='';
        //dump(I('post.detailPic'));die;
        foreach(I('post.detailPic') as $k=>$v){
            $image = new \Think\Image();
            $image->open($v);// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
            $smallPath = dirname($v) .'/small_'.basename($v);
            $mediumPath = dirname($v) .'/medium_'.basename($v);
            $bigPath = dirname($v) .'/big_'.basename($v);
            $small .= $smallPath .'|';
            $medium .= $mediumPath . '|';
            $big .=$bigPath .'|';
            $image->thumb(50, 50)->save($smallPath);
            $image->thumb(600,600)->save($mediumPath);
            $image->thumb(800,800)->save($bigPath);

        }

//        dump(rtrim($small,'|'));
        $data['small'] = rtrim($small, '|');
        $data['medium'] = rtrim($medium, '|');
        $data['big'] = rtrim($big, '|');
        $data['gid'] = $gid;
        $data['intro'] = I('post.intro');
        $data['service'] = I('post.service');
//        dump($data['small']);die;
        $detailModel->add($data);
        //添加商品属性
        $arrtModel = new GoodsAttrModel();
        //属性添加
        foreach($_POST['attr'] as $k=>$v){
            if($v){
                $data['gtvalue'] = $v;
                $data['added'] = 0;
                $data['aid'] = $k;
                $data['gid'] =$gid;
                $arrtModel->add($data);
            }
        }
        //规格添加
        foreach($_POST['spec'] as $k=>$v){
            foreach($v['spec'] as $kk=>$vv){
                $data['gtvalue'] = $vv;
                $data['added'] = $v['added'][$kk];
                $data['aid'] = $k;
                $data['gid'] = $gid;
                $arrtModel->add($data);
            }
        }
        return ['valid'=>ture,'message'=>'添加成功'];
    }
    public function edit(){
        if (!$this->create()) {
            return ['valid' => false, 'message' => $this->getError()];
        }
        $detailModel = new DetailModel();
        if (!$detailModel->create($_POST)) {
            return ['valid' => false, 'message' => $detailModel->getError()];
        }
        //验证图册是否上传
        if(!isset($_POST['detailPic'])){
            return ['valid'=>false,'message'=>'请上传商品图册'];
        }
        $gid = I('get.gid');
        //添加商品表
        $data['gid'] = $gid;
//        dump($data['gid']);die;
        $data['gname'] = I('post.gname');
        $data['gnumber'] = I('post.gnumber');
        $data['unit'] = I('post.unit');
        $data['marketprice'] = I('post.marketprice');
        $data['shopprice'] = I('post.shopprice');
        $data['total'] = I('post.total');
        $data['pic'] = I('post.pic');
        $data['click'] = I('post.click');
        $data['time'] = time();
        $data['bid'] = I('post.bid');
        $data['cid'] = I('post.cid');
        $data['tid'] = I('post.tid');
        $data['aid'] = 1;//管理员ID 取自session
        $this->where("gid = {$gid}")->save($data);
        //添加商品详情表
        $small ='';
        $medium ='';
        $big ='';
        //dump(I('post.detailPic'));die;
        foreach(I('post.detailPic') as $k=>$v){
            $image = new \Think\Image();
            $image->open($v);// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
            $smallPath = dirname($v) .'/small_'.basename($v);
            $mediumPath = dirname($v) .'/medium_'.basename($v);
            $bigPath = dirname($v) .'/big_'.basename($v);
            $small .= $smallPath .'|';
            $medium .= $mediumPath . '|';
            $big .=$bigPath .'|';
            $image->thumb(50, 50)->save($smallPath);
            $image->thumb(60,60)->save($mediumPath);
            $image->thumb(80,80)->save($bigPath);

        }

//        dump(rtrim($small,'|'));
        $data['small'] = rtrim($small, '|');
        $data['medium'] = rtrim($medium, '|');
        $data['big'] = rtrim($big, '|');
        $data['intro'] = I('post.intro');
        $data['service'] = I('post.service');
//        dump($data['small']);die;
        $detailModel->save($data);
        //添加商品属性
        $arrtModel = new GoodsAttrModel();
        $arrtModel -> where("gid = {$gid}")->delete();
        //属性添加
        foreach($_POST['attr'] as $k=>$v){
            if($v){

                $data['gtvalue'] = $v;
                $data['added'] = 0;
                $data['aid'] = $k;
                $data['gid']=$gid;
                $arrtModel->add($data);
            }
        }
//        p($_POST['spec']);die;
        //规格添加
        foreach($_POST['spec'] as $k=>$v){
            foreach($v['spec'] as $kk=>$vv){
                $data['gtvalue'] = $vv;
                $data['added'] = $v['added'][$kk];
                $data['aid'] = $k;
                $data['gid']=$gid;
                $arrtModel->add($data);
            }
        }
        return ['valid'=>ture,'message'=>'修改成功'];

    }

}